function addEvent(evnt, elem, func) {
   if (elem.addEventListener)
      elem.addEventListener(evnt,func,false);
   else if (elem.attachEvent) {
      elem.attachEvent("on"+evnt, func);
   }
   else {
      elem[evnt] = func;
   }
}

function testBrowser() {
  this.userAgent = navigator.userAgent;
  this.isIE = window.ActiveXObject || "ActiveXObject" in window;
  this.isEdge = this.userAgent.indexOf("Edge") > -1;
  this.isFF = this.userAgent.indexOf("Firefox") > -1;
  this.isOpera = this.userAgent.indexOf("Opera") > -1;
  this.isSafari = this.userAgent.indexOf("Safari") > -1 && this.userAgent.indexOf("Chrome") == -1;
  this.isChrome = this.userAgent.indexOf("Chrome") > -1 && this.userAgent.indexOf("Safari") > -1 && !this.isEdge;
}

function AddFavorite() {
  this.tb = new testBrowser();
  this.h1 = document.getElementsByTagName('h1')[0];
  this.el = document.getElementById('addFavorite');
  if (this.h1) {
    this.brands = this.h1.innerText.substring(0, this.h1.innerText.indexOf('_')) + '品牌招商专区';
    this.init()
  }
}

AddFavorite.prototype = {
  init: function () {
    if (this.el) {
      this.setFavoriteTitle();
      this.setFavorite();
    }
  },
  setFavoriteTitle: function () {
    if (!this.el.getAttribute('title')) {
      this.el.setAttribute('title', this.brands);
    }
  },
  setFavorite: function () {
    var _this = this;
    this.el.setAttribute('rel', 'sidebar');
    addEvent('click', this.el, function () {
      _this.addFavorite(_this);
    })
  },
  addFavorite: function (_this) {
    _this.brands = _this.el.getAttribute('title') || _this.brands;
    if (_this.tb.isFF) {
      window.sidebar.addPanel(_this.brands, window.location.href , "");
    } else if (_this.tb.isIE) {
      window.external.addFavorite(window.location.href, _this.brands);
    } else if (_this.tb.isSafari || _this.tb.isChrome || _this.tb.isOpera) {
      alert('按下 Ctrl + D 键收藏本页面');
    }
  }
}

new AddFavorite();

function addFavorite() {
  alert('按下 Ctrl + D 键收藏本页面');
}
